{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "from bs4 import BeautifulSoup\n",
    "from tqdm import tqdm\n",
    "import time\n",
    "import csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "jd_comment_urls = ['https://club.jd.com/comment/productPageComments.action?productId=251837&score=0&sortType=6&page=%d&pageSize=10' % (i) for i in range(100)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "headers = {\n",
    "    'User-Agent': 'Mozilla/5.0'\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def get_page(url):\n",
    "    try:\n",
    "        r = requests.get(url, headers=headers)\n",
    "        r.raise_for_status\n",
    "        return r.json()\n",
    "    except:\n",
    "        print(url)\n",
    "        return"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 78%|███████████████████████████████▉         | 78/100 [00:21<00:07,  2.95it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "https://club.jd.com/comment/productPageComments.action?productId=251837&score=0&sortType=6&page=78&pageSize=10\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|████████████████████████████████████████| 100/100 [00:29<00:00,  3.13it/s]\n"
     ]
    }
   ],
   "source": [
    "comments = []\n",
    "for url in tqdm(jd_comment_urls):\n",
    "    data = get_page(url)\n",
    "    try:\n",
    "        comments += data['comments']\n",
    "    except:\n",
    "        continue"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "nickname = [comment['nickname'] for comment in comments]\n",
    "content = [comment['content'] for comment in comments]\n",
    "productColor = [comment['productColor'] for comment in comments]\n",
    "referenceName = [comment['referenceName'] for comment in comments]\n",
    "creationTime = [comment['creationTime'] for comment in comments]\n",
    "days = [comment['days'] for comment in comments]\n",
    "referenceTime = [comment['referenceTime'] for comment in comments]\n",
    "score = [comment['score'] for comment in comments]\n",
    "replyCount = [comment['replyCount'] for comment in comments]\n",
    "usefulVoteCount = [comment['usefulVoteCount'] for comment in comments]\n",
    "uselessVoteCount = [comment['uselessVoteCount'] for comment in comments]\n",
    "isMobile = [comment['isMobile'] for comment in comments]\n",
    "userClient = [comment['userClient'] for comment in comments]\n",
    "userClientShow = [comment['userClientShow'] for comment in comments]\n",
    "userLevelName = [comment['userLevelName'] for comment in comments]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|█████████████████████████████████████| 984/984 [00:00<00:00, 20485.81it/s]\n"
     ]
    }
   ],
   "source": [
    "date = time.strftime('%Y%m%d',time.localtime(time.time()))\n",
    "with open(r'data/jd_comments%s.csv' % (date), 'w', newline='') as f:\n",
    "    writer = csv.writer(f)\n",
    "    for i in tqdm(range(len(nickname))):\n",
    "        writer.writerow([nickname[i], content[i], creationTime[i], referenceName[i], referenceTime[i], userClientShow[i], userLevelName[i], days[i], isMobile[i], productColor[i],  replyCount[i], score[i], usefulVoteCount[i], uselessVoteCount[i], userClient[i]])"
   ]
  }
 ],
 "metadata": {
  "hide_input": false,
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  },
  "toc": {
   "colors": {
    "hover_highlight": "#DAA520",
    "navigate_num": "#000000",
    "navigate_text": "#333333",
    "running_highlight": "#FF0000",
    "selected_highlight": "#FFD700",
    "sidebar_border": "#EEEEEE",
    "wrapper_background": "#FFFFFF"
   },
   "moveMenuLeft": true,
   "nav_menu": {
    "height": "12px",
    "width": "252px"
   },
   "navigate_menu": true,
   "number_sections": true,
   "sideBar": true,
   "threshold": 4,
   "toc_cell": false,
   "toc_section_display": "block",
   "toc_window_display": false,
   "widenNotebook": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
